package com.aiyige.share.selectcontact;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.ContactResp;
import com.aiyige.model.Page;
import com.aiyige.share.model.ShareContactItem;
import com.aiyige.share.selectcontact.adapter.ContactAdapter;
import com.aiyige.share.selectcontact.eventbus.EventSelectShareContactItem;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.SelectContactPage)
public class SelectContactPage extends AppCompatActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.cdv)
    CommonDataView cdv;
    ContactAdapter contactAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_select_contact);
        ButterKnife.bind(this);
        contactAdapter = new ContactAdapter();
        contactAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ShareContactItem contact = contactAdapter.getItem(position);
                EventBus.getDefault().postSticky(EventSelectShareContactItem.newBuilder()
                        .shareContactItem(contact)
                        .build());
                finish();
            }
        });
        cdv.config(contactAdapter, this);
        cdv.doRefreshRequest();

    }

    @OnClick({R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancelBtn:
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().mutualFans(currentPage, 15);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<ShareContactItem> shareContactItemList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                List<ContactResp> tempContactRespList = JSON.parseArray(page.getContent(), ContactResp.class);
                for (ContactResp contactResp : tempContactRespList) {
                    ContactResp safeContactResp = new ContactResp(contactResp);
                    shareContactItemList.add(ShareContactItem.newBuilder()
                            .accid(safeContactResp.getAccid())
                            .avatar(safeContactResp.getAvatar())
                            .id(safeContactResp.getUserId())
                            .name(safeContactResp.getAccount())
                            .build());
                }
            }
            resultList.addAll(shareContactItemList);
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
